package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.ManySheetOneSyler;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsReportsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/31.
 */
@RestController
@RequestMapping("/reports")
@Api(value = "/reports", description = "报表")
public class WmsReportsController  extends BaseController<WmsReportsService,WmsHarvestDaily> {

    @PostMapping(value = "/baseHarvestPage")
    @ApiOperation(value = "收货日报表",response = JSONObject.class, notes = "收货日报表分页查询")
    public @ResponseBody
    JSONObject  baseHarvestPage(@RequestBody WmsHarvestDaily entity, HttpSession session) {
        Page<WmsHarvestDaily> list =new Page<WmsHarvestDaily>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            list= baseService.baseHarvestPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "导出收货日报表", response = JSONObject.class, notes = "导出收货日报表")
    @PostMapping(value = "getOutPutHarvest")
    public JSONObject getOutPutHarvest(HttpServletResponse response, @RequestBody Map map, HttpSession session) throws IOException {
        String filename = "收货日报表导出";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        map.put("ckid", ckid);
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid=hz.get(0).getHzid();
        map.put("hzid",hzid);
        List<String> ids = (List) map.get("ids");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("收货日报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsHarvestDaily> list = null;

        list = baseService.getOutPutHarvest(ids, map);

        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsHarvestDaily.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }

    @PostMapping(value = "/baseWmsShelvesDailiesPage")
    @ApiOperation(value = "上架日报表",response = JSONObject.class, notes = "上架日报表分页查询")
    public @ResponseBody
    JSONObject  baseWmsShelvesDailiesPage(@RequestBody WmsShelvesDailies entity, HttpSession session) {
        Page<WmsShelvesDailies> list =new Page<WmsShelvesDailies>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            list= baseService.baseWmsShelvesDailiesPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "上架日报表导出", response = JSONObject.class, notes = "上架日报表导出")
    @PostMapping(value = "getOutPutShelves")
    public JSONObject getOutPutShelves(HttpServletResponse response,@RequestBody Map map, HttpSession session) throws IOException {
        String filename = "上架日报表导出";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        map.put("ckid", ckid);
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid=hz.get(0).getHzid();
        map.put("hzid",hzid);
        List<String> ids = (List) map.get("ids");

        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("上架日报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsShelvesDailies> list = null;

        list = baseService.getOutPutShelves(ids, map);

        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsShelvesDailies.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }

    //货品库存总数量
    @ApiOperation(value = "货品库存总数量", response = JSONObject.class, notes = "货品库存总数量")
    @GetMapping(value = "quantity")
     public JSONObject quantity(){
        List<WmsStock> list=null;
         JSONObject json = getJSONObject();
         try {
             baseService.quantity();
             json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                     ,(int) list.size());
         }catch (Exception e){
             e.printStackTrace();
             json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"货品库存总数量失败",e.getMessage(),"");
         }
         return json;
     }

    @PostMapping(value = "/outStock")
    @ApiOperation(value = "进出存合并报表查询",response = JSONObject.class, notes = "进出存合并报表分页查询")
    public @ResponseBody
    JSONObject  outStock(@RequestBody WmsOutStock entity, HttpSession session) {
        Page<WmsOutStock> list =new Page<WmsOutStock>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            list= baseService.outStock(entity);
            if (list !=null){
                json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                        ,(int) list.getTotal());
            }else {
                json = SetStatus.setSearchType(new JSONArray().toString(),0);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "进出存合并报表导出", response = JSONObject.class, notes = "进出存合并报表导出")
    //@PostMapping(value = "getoutStock")
    @GetMapping(value = "getoutStock")
    public JSONObject getoutStock(HttpServletResponse response,/*@RequestBody Map map,*/ HttpSession session) throws IOException,Exception {
         Map map=new HashMap();
         String filename = "进出存合并报表导出";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        map.put("ckid", ckid);
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid=hz.get(0).getHzid();
        map.put("hzid",hzid);
        List<String> ids = (List) map.get("ids");

        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("进出存合并报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsOutStock> list = null;
        list = baseService.getoutStock(ids, map);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsOutStock.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }

    @PostMapping(value = "/baselibraryPage")
    @ApiOperation(value = "出库日报表",response = JSONObject.class, notes = "出库日报表分页查询")
    public @ResponseBody
    JSONObject  baselibraryPage(@RequestBody Wmslibrary entity, HttpSession session) {
        Page<Wmslibrary> list =new Page<Wmslibrary>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            list= baseService.baselibraryPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "导出出库日报表", response = JSONObject.class, notes = "导出出库日报表")
    @PostMapping(value = "getoutlibrary")
    public JSONObject getoutlibrary(HttpServletResponse response, @RequestBody Map map, HttpSession session) throws IOException {
         String filename = "出库日报表导出";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        map.put("ckid", ckid);
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid=hz.get(0).getHzid();
        map.put("hzid",hzid);
        List<String> ids = (List) map.get("ids");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("收货日报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<Wmslibrary> list = null;

        list = baseService.getoutlibrary(ids, map);

        Workbook workbook = ExcelExportUtil.exportExcel(ep, Wmslibrary.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }
    //货品库存总数量
    @ApiOperation(value = "合计统计数量", response = JSONObject.class, notes = "合计统计数量")
    @GetMapping(value = "/countSlByRq/{sj}")
    public JSONObject countSlByRq(@PathVariable("sj") String sj,HttpSession session){
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
            json = baseService.countTjBySj(sj,ckid);
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"合计统计数量失败",e.getMessage(),"");
        }
        return json;
    }

    /**
     * 货品出货量
     */
    @ApiOperation(value = "货品出货量", response = JSONObject.class, notes = "货品出货量")
    @PostMapping(value = "/shipmentGoods")
    public JSONArray shipmentGoods(@RequestBody WmsPicking entity,HttpSession session){
        JSONArray jsonArray = new JSONArray();
        JSONObject json=null;
        try {
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            List<WmsPicking> list= baseService.shipmentGoods(entity);
            if (list.size()>0){
                for (int i=0;i<list.size();i++){
                    json = new JSONObject();
                    json.put("hpmc",list.get(i).getHpmc());
                    json.put("cksl",list.get(i).getSjsl());
                    jsonArray.add(json);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
         }
        return jsonArray;
    }

    @PostMapping(value = "/getstock")
    @ApiOperation(value = "库存报表查询",response = JSONObject.class, notes = "库存报表分页查询")
    public @ResponseBody
    JSONObject  getstock(@RequestBody WmsStockReport entity, HttpSession session) {
        Page<WmsStockReport> list =new Page<WmsStockReport>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            if (entity.getKcsl()==null){
                entity.setKcsl("");
            }
            list= baseService.getstock(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "导出库存报表", response = JSONObject.class, notes = "导出库存报表")
    @PostMapping(value = "getstockout")
    public JSONObject getstockout(HttpServletResponse response, @RequestBody Map map, HttpSession session) throws IOException {
        String filename = "库存报表导出";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        map.put("ckid", ckid);
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid=hz.get(0).getHzid();
        map.put("hzid",hzid);
        List<String> ids = (List) map.get("ids");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("库存报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsStockReport> list = null;
        list = baseService.getstockout(ids, map);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsStockReport.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }

}
